package com.example.gulimall.common.utils;

import com.example.gulimall.common.enums.ServiceCodeEnum;
import com.example.gulimall.common.exceptions.ApiException;
import lombok.Data;

import java.io.Serializable;

/**
 * <p>DESC:业务返回参数统一封装 </p>
 * <p>DATE: 2019/1/30</p>
 * <p>VERSION:1.0.0</p>
 * <p>@AUTHOR: JGD</p>
 */
@Data
public class JsonResultVO<T> implements Serializable {
    private static final long serialVersionUID = -7581376698660757259L;
    /**
     * 响应code
     */
    private Integer code;

    /**
     * 响应信息
     */

    private String msg;

    /**
     * 响应数据
     */
    private T data;

    public JsonResultVO(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public JsonResultVO(Integer code) {
        this.code = code;
    }

    public JsonResultVO(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public JsonResultVO(T data) {
        this.code = 0;
        this.msg = "成功";
        this.data = data;
    }

    public JsonResultVO() {
        this.code = 0;
        this.msg = "成功";
    }

    /**
     * 抛出异常
     *
     * @return T
     */
    public T invoke() {
        if (!ServiceCodeEnum.SUCCESS.getCode().equals(code)) {
            throw new ApiException(code, msg);
        }
        return data;
    }
}
